package com.atguigu.gmall.seckill.controller;

import com.atguigu.gmall.common.config.utils.UserAuthUtil;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.order.vo.OrderSubmitVo;
import com.atguigu.gmall.seckill.service.SeckillBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-26 14:29
 **/
@RequestMapping("/api/activity/seckill/auth")
@RestController
public class SeckillApiController {
    @Autowired
    SeckillBizService seckillBizService;
    /**
     * 获取秒杀码
     * http://api.gmall.com/api/activity/seckill/auth/getSeckillSkuIdStr/49
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillCode(@PathVariable("skuId") Long skuId) {
        // 得到秒杀码
        String code = seckillBizService.generateSeckillCode(skuId);
        return Result.ok(code);
    }

    /**
     * 下秒杀单（抢单）
     *
     * @param skuId
     * @param skuIdStr
     * @return
     */
    // http://api.gmall.com/api/activity/seckill/auth/seckillOrder/50?skuIdStr=b94c77f9066dbc8350ac9254c3858e20
    @PostMapping("/seckillOrder/{skuId}")
    public Result saveSecKillOrder(@PathVariable("skuId") String skuId,
                                   @RequestParam("skuIdStr") String skuIdStr) {
        // 通过秒杀ID和秒杀码抢单
        seckillBizService.seckillOrder(skuId, skuIdStr);
        return Result.ok();
    }

    // http://api.gmall.com/api/activity/seckill/auth/checkOrder/50

    /**
     * 检查秒杀单状态
     *
     * @param skuId
     * @return
     */
    @GetMapping("/checkOrder/{skuId}")
    public Result checkOrder(@PathVariable("skuId") Long skuId) {
        String userId = UserAuthUtil.getUserIdHeader();
        /**
         * 211(排队中) 215(抢购成功去下单) 218(抢购成功我的订单)
         *  - 211：库存还没有扣成，正在mq消息队列中排队
         *  - 215：完善订单信息：收货地址等【去订单确认页，完善收货地址】
         *  - 218：已经秒杀过了且订单已经生成了【点击 我的订单按钮 直接去看已经秒成的订单】
         */
        ResultCodeEnum codeEnum = seckillBizService.checkOrder(skuId, userId);
        return Result.build("", codeEnum);
    }

    /**
     * 提交秒杀订单
     *
     * @param vo
     * @return
     */
    // http://api.gmall.com/api/activity/seckill/auth/submitOrder
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitVo vo) {


        Long orderId = seckillBizService.submitOrder(vo);


        return Result.ok(orderId.toString());
    }
}
